import axios from 'axios'
import {
  message
} from 'ant-design-vue';
import env from "../config/env";


import router from '../router'

let host = window.location.host;
let ip = 'https://' + host.split(":")[0] + ':3333'

axios.defaults.baseURL = env === "development" ?
ip : "http://47.119.158.229:3333";

// axios.defaults.withCredentials = true
axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest'
axios.defaults.headers['token'] = localStorage.getItem('token') || ''
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
// axios.defaults.headers.post['Content-Type'] = 'multipart/form-data'
axios.defaults.headers.get['Content-Type'] = 'application/x-www-form-urlencoded';
// axios.defaults.headers.get['Content-Type'] = 'application/x-www-form-urlencoded';

axios.defaults.transformRequest = [function (data) {
  let ret = ''
  for (let it in data) {
    ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
  }
  return ret
}]


axios.interceptors.request.use(config => {
  const token = window.sessionStorage.getItem('token')
  if (config.method == 'get' && token || config.method == 'post' && token) {
    if (!config.params) {
      config.params = {}
    }
    config.params.token = token
  }
  return config
})

axios.interceptors.response.use(res => {
  if (res.data.status == 0) {
    message.error(res.data.body);
  }

  return res.data
})

export default axios